﻿namespace OpenComposite.Inbox.Forms
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Windows.Forms;

	using OpenComposite.Base.Structures;
	using OpenComposite.HumanActivity.Contracts;

	partial class FormNotes : Form
	{
		#region Initialization

		public FormNotes(ActivityItem activityItem, IHumanActivityFormService formService)
		{
			InitializeComponent();

			this.ActivityItem = activityItem;
			this.FormService = formService;

			if ( activityItem != null ) {
				this.lbProcNm.Text =
					String.Format("{0} - {1}: {2} (ID: {3})",
						activityItem.ProcessName, activityItem.ActivityName,
						activityItem.Subject, activityItem.ActivityId);
				initialize();
			}
			setUIForNewNote(false);
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		public ActivityItem ActivityItem
		{
			get;
			private set;
		}

		public IHumanActivityFormService FormService
		{
			get;
			private set;
		}

		#endregion Properties

		#endregion Public Members

		#region Private Members

		#region Fields

		private ArrayList _requestedGenericInstances = new ArrayList();
		private TreeNode _rootNode;

		#endregion Fields

		#region Event Handlers

		private void _btnAddNote_Click(object sender, EventArgs e)
		{
			txtSubject.Text = "";
			txtNoteText.Text = "";
			setUIForNewNote(true);
			txtSubject.Focus();
		}

		private void _btnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void _tvNotes_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if ( e.Node == null ) return; // EXIT
			NoteItem note = e.Node.Tag as NoteItem;
			setUIForNewNote(false);
			if ( note != null ) {
				txtSubject.Text = note.Subject;
				txtNoteText.Text = note.Text;
			} else {
				txtSubject.Text = "";
				txtNoteText.Text = "";
			}
		}

		private void btnAddNoteCancel_Click(object sender, EventArgs e)
		{
			setUIForNewNote(false);
			txtSubject.Text = "";
			txtNoteText.Text = "";
		}

		private void btnAddNoteOK_Click(object sender, EventArgs e)
		{
			if ( txtSubject.TextLength == 0 ) {
				MessageBox.Show("Please enter a subject for the new note!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				txtSubject.Focus();
				return; // EXIT
			} else if ( txtNoteText.TextLength == 0 ) {
				MessageBox.Show("Please enter a text for the new note!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				txtNoteText.Focus();
				return; // EXIT
			} else {
				addNote();
			}
			setUIForNewNote(false);
		}

		#endregion Event Handlers

		#region Methods

		private void addNote()
		{
			try {
				NoteItem note = new NoteItem(
					this.ActivityItem.WorkflowInstanceId,
					this.ActivityItem.ActivityId,
					this.ActivityItem.ActivityUserId.Value);

				note.Text = txtNoteText.Text;
				note.Subject = txtSubject.Text;

				this.FormService.SetNote(note);

				this.txtNoteText.Text = "";
				this.txtSubject.Text = "";

				loadNotes();
			} catch ( Exception ex ) {
				string msg = String.Format("Error while adding new note.{0}({1})", Environment.NewLine, ex.Message);
				MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void initialize()
		{
			this._rootNode = new TreeNode();
			_rootNode.Text = "Process Notes";
			this.tvNotes.Nodes.Add(_rootNode);
			loadNotes();
		}

		private void loadNotes()
		{
			this._rootNode.Nodes.Clear();
			List<NoteItem> notes = this.FormService.GetNotes(this.ActivityItem.ActivityId.Value);
			foreach ( var note in notes ) {
				TreeNode tn = new TreeNode();
				tn.Tag = note;
				tn.ImageIndex = 3;
				tn.SelectedImageIndex = 3;
				tn.Text = note.Subject;
				this._rootNode.Nodes.Add(tn);
			}
			this._rootNode.Expand();
		}

		private void setUIForNewNote(bool isNewNote)
		{
			txtSubject.ReadOnly = !isNewNote;
			txtNoteText.ReadOnly = !isNewNote;
			btnAddNoteOK.Enabled = isNewNote;
			btnAddNoteCancel.Enabled = isNewNote;
			btnAddNote.Enabled = !isNewNote;
		}

		#endregion Methods

		#endregion Private Members
	}
}